package com.marketapp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.marketapp.domain.Equity;
import com.marketapp.helper.ServiceException;
import com.marketapp.service.IEquityService;
import com.marketapp.util.StringUtil;
import com.marketapp.validators.EquitySearchValidator;
import com.marketapp.validators.EquityValidator;

@Controller
@RequestMapping(value = "/equityquery")
public class EquityDetailQueryController {

	@Autowired
	IEquityService equityService;
	
	public IEquityService getEquityService() {
		return equityService;
	}

	public void setEquityService(IEquityService equityService) {
		this.equityService = equityService;
	}

	@Autowired
	EquitySearchValidator validator;

	public EquitySearchValidator getValidator() {
		return validator;
	}

	public void setValidator(EquitySearchValidator validator) {
		this.validator = validator;
	}

	public IEquityService getEquityDetailsService() {
		return equityService;
	}

	public void setEquityDetailsService(IEquityService equityDetailsService) {
		this.equityService = equityDetailsService;
	}

	@RequestMapping(method = RequestMethod.GET)
	public void setUpForm(Model model) {
		Equity equity = new Equity();
		model.addAttribute("equity",equity);
	}

	@RequestMapping(method = RequestMethod.POST)
	public String submitForm(@ModelAttribute(value = "equity") Equity equity,
			BindingResult result, Model model) {
		try {
			validator.validate(equity, result);
			if(result.hasErrors()){
				return "equityquery";
			}
			List<Equity> resultEquity = equityService
					.getEquityDetailsbyName(equity.getEquityName());
			model.addAttribute("resultList", resultEquity);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return "equityquery";
	}
}
